# HTML DOM insertBefore() Method

Element Object

## Example

Insert a new <li> element before the first child element of an <ul> element:

var newItem = document.createElement("LI") // Create a <li> node
var textnode = document.createTextNode("Water") // Create a text node
newItem.appendChild(textnode) // Append the text to <li>
var list = document.getElementById("myList") // Get the <ul> element to insert a new node
list.insertBefore(newItem, list.childNodes[0]); // Insert <li> before the first child of <ul>

Before inserting:

- Coffee
- Tea

After inserting:

- Water
- Coffee
- Tea

Try it yourself »

More "Try it Yourself" examples below.

## Definition and Usage

The insertBefore() method inserts a node as a child, right before an existing child, which you specify.

Tip: If you want to create a new list item, with text, remember to create the text as a Text node which you append to the <li> element, then insert <li> to the list.

You can also use the insertBefore method to insert/move an existing element (See "More Examples").

## Browser Support

The numbers in the table specify the first browser version that fully supports the method.

Method |  |  |  |  |
--- | --- | --- | --- | --- | ---
insertBefore() | 1.0 | Yes | 1.0 | Yes | Yes

## Syntax

node.insertBefore(newnode,existingnode)

## Parameter Values

Parameter | Type | Description
--- | --- | ---
newnode | Node object | Required. The node object you want to insert
existingnode | Node object | Optional. The child node you want to insert the new node before. When not specified, the insertBefore method will insert the newnode at the end

## Technical Details

Return Value: | A Node Object, representing the inserted node
DOM Version | Core Level 1 Node Object

## More Examples

## Example

Move a <li> element from one list to another:

var node = document.getElementById("myList2").lastChild;
var list = document.getElementById("myList1");
list.insertBefore(node, list.childNodes[0]);

Before inserting:

- Coffee
- Tea

- Water
- Milk

After insertBefore:

- Milk
- Coffee
- Tea

- Water

Try it yourself »

Element Object

### WEB HOSTING

UK Reseller Hosting

### WEB BUILDING

FREE Website BUILDER Free HTML5 Templates

### W3SCHOOLS EXAMS

HTML, CSS, JavaScript, PHP, jQuery, XML, and ASP Certifications

### SHARE THIS PAGE

### Color Picker

### HTML/CSS

HTML Tutorial HTML5 Tutorial CSS Tutorial CSS3 Tutorial Bootstrap Tutorial

### JavaScript

JavaScript Tutorial jQuery Tutorial jQuery Mobile Tutorial AngularJS Tutorial AJAX Tutorial JSON Tutorial

### Graphics

Canvas Tutorial SVG Tutorial Google Maps Tutorial

### Server Side

SQL Tutorial PHP Tutorial ASP Tutorial ASP.NET Tutorial VBScript Tutorial AppML Tutorial

### Web

Web Building Web Statistics Web Certification

### XML

XML Tutorial DTD Tutorial Schema Tutorial XML DOM Tutorial XPath Tutorial XSLT Tutorial XQuery Tutorial XSL-FO Tutorial RSS Tutorial WSDL Tutorial

### HTML/CSS

HTML Tag Reference HTML Event Reference HTML Color Reference CSS 1,2,3 Reference Bootstrap Reference

### JavaScript

JavaScript Reference HTML DOM Reference jQuery Reference jQuery Mobile Reference Google Maps Reference

### Server Side

PHP Reference SQL Reference ASP Reference VBScript Reference Razor Reference ASP.NET Reference

### XML

XML DOM Reference XSLT Reference XSL-FO Reference XPath Reference XQuery Reference Schema Reference RSS Reference SVG Reference

### HTML Charsets

HTML Character Sets HTML ASCII HTML ANSI HTML Windows-1252 HTML ISO-8859-1 HTML Symbols HTML UTF-8

### HTML/CSS

HTML Examples CSS Examples

### Browser Scripting

JavaScript Examples HTML DOM Examples jQuery Examples jQuery Mobile Examples AngularJS Examples AJAX Examples

### Server Scripting

PHP Examples ASP Examples VBScript Examples Razor Examples .NET Examples

### XML Languages

XML Examples DTD Examples XML DOM Examples XSL Examples XSLT Examples XPath Examples XQuery Examples Schema Examples RSS Examples SVG Examples